package com.qf.smsplatfrom.sucurity;

import com.qf.smsplatfrom.mapper.UserMapper;
import com.qf.smsplatfrom.pojo.MyAuthUser;
import com.qf.smsplatfrom.pojo.TAdminUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class MyUserDetailsService implements UserDetailsService {
    private UserMapper userMapper;

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        TAdminUser user = userMapper.selectByName(username);//从数据库中查询到当前用户的信息
        MyAuthUser myAuthUser = new MyAuthUser(username,user.getPassword(),new ArrayList<>());//将用户信息反馈给数据他将数据进行比对
        myAuthUser.setId(user.getId());//将用户的id存入到myAuthUser中，方便其返回，下边同上
        myAuthUser.setType(user.getType());
        return myAuthUser;//返回存好值得对象
    }
}
